package edu.uw.tcss360.group6.gui.MainCards;

import edu.uw.tcss360.group6.model.*;


import java.awt.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;



/**
 * Author: David Sharer
 */
@SuppressWarnings("serial")
public class WeavingDraftPanel extends MainCard {
    private WeavingDraft my_weaving_draft       = new WeavingDraft();
    private static final Color OUTLINE_COLOR    = Color.GREEN.darker();
    private static final Color FILL_COLOR       = Color.BLACK;
    private final WDMouseListener wdMouseListener;

    public WeavingDraftPanel() {
        super();
        wdMouseListener = new WDMouseListener();
        this.setBackground(Color.WHITE);
        this.addMouseListener(wdMouseListener);
        this.addMouseMotionListener(wdMouseListener);
        
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //draw grids


        int x_offset = 10;
        int y_offset = 10;
        //first up is the horizontal grid
        for(int i = 0; i < my_weaving_draft.getWidth(); i++) {
            int type = my_weaving_draft.getHorizontal(i);
            for(int j = 0; j < my_weaving_draft.getMultiplicity(); j++) {
                g.setColor(FILL_COLOR);
                if(my_weaving_draft.getMultiplicity()- j == type) //got rid of the minus 1 and inverted of getMult. Bill
                    g.fillRect(x_offset+i*10,y_offset+j*10,10,10);
                g.setColor(OUTLINE_COLOR);
                g.drawRect(x_offset+i*10,y_offset+j*10,10,10);
            }
            g.drawString(Integer.toString(type),x_offset+i*10+1,y_offset+11);
        }


        x_offset = 10*my_weaving_draft.getWidth() + 10+10;
        y_offset = 10*my_weaving_draft.getMultiplicity() + 10+10;
        //second is the vertical grid
        for(int i = 0; i < my_weaving_draft.getHeight(); i++) {
            int index_i = i;
            //g.drawString(Integer.toString(type),0+i*10+1,11);
            for(int j = 0; j < my_weaving_draft.getMultiplicity(); j++) {
                int index_j = j+1;
                g.setColor(FILL_COLOR);
                if(my_weaving_draft.getVertical(index_i) == index_j) {
                    g.fillRect(x_offset+j*10,y_offset+i*10,10,10);
                    g.setColor(OUTLINE_COLOR);
                    g.drawString(Integer.toString(index_j),x_offset+j*10,y_offset+i*10+11);
                }
                g.setColor(OUTLINE_COLOR);
                g.drawRect(x_offset+j*10,y_offset+i*10,10,10);
            }
        }


        x_offset = 10*my_weaving_draft.getWidth() + 10+10;
        y_offset = 10;
        //third is the mapping grid
        for(int i = 0; i < my_weaving_draft.getMultiplicity(); i++) {
            g.drawString(Integer.toString(i+1),x_offset+i*10+2,10*my_weaving_draft.getMultiplicity() + 10+10);
            int index_i = i+1;
            for(int j = 0; j < my_weaving_draft.getMultiplicity(); j++) {
                int index_j = my_weaving_draft.getMultiplicity()-j;
                if(i == 0)
                    g.drawString(Integer.toString(index_j),x_offset-8,y_offset+j*10+10);
                g.setColor(FILL_COLOR);
                if(my_weaving_draft.getMapping(index_i,index_j))
                    g.fillRect(x_offset+i*10,y_offset+j*10,10,10);
                g.setColor(OUTLINE_COLOR);
                g.drawRect(x_offset+i*10,y_offset+j*10,10,10);
            }
        }



        x_offset = 10;
        y_offset = 10*my_weaving_draft.getMultiplicity() + 10+10;
        //the last is the result area
        for(int i = 0; i < my_weaving_draft.getWidth(); i++) {
            //g.drawString(Integer.toString(type),0+i*10+1,11);
            for(int j = 0; j < my_weaving_draft.getHeight(); j++) {
                //the get output color method is throwing an error so commented out for now
            	g.setColor(my_weaving_draft.getOutputColor(i, j));
                g.fillRect(x_offset + i * 10, y_offset + j * 10, 10, 10);
                g.setColor(OUTLINE_COLOR);
                g.drawRect(x_offset + i * 10, y_offset + j * 10, 10, 10);
            }
        }
    }
    
    
    /*
     * author bill minter
     * depending on the region clicked, calls the methods to update the weaving draft.
     */
   private void clickedSquare(int x, int y, boolean set) {
       //left column
	   if(x >= 10 && x < 10 + (my_weaving_draft.getWidth() * 10)) {
           //top
		   if(y >= 10 && y < 10 + (my_weaving_draft.getMultiplicity() * 10)) { //in the horizontal
               if(set)
			        my_weaving_draft.setHorizontal((x-10)/10, my_weaving_draft.getMultiplicity()-(y-10)/10);
		   }
        //right column
	   } else if(x >= 20 + (my_weaving_draft.getWidth() * 10) &&
	   			 x < 20 + ((my_weaving_draft.getWidth() + my_weaving_draft.getMultiplicity()) * 10)) {
           //corner
		   if(y >= 10 && y < 10 + (my_weaving_draft.getMultiplicity() * 10)) { //in the corner
               if(set) {
			        my_weaving_draft.addMapping((x-20-(my_weaving_draft.getWidth() * 10))/10+1,
                       my_weaving_draft.getMultiplicity()-(y-10)/10); //Adjusted to fit new algorithm + display - David
               } else {
                    my_weaving_draft.removeMapping((x-20-(my_weaving_draft.getWidth() * 10))/10+1,
                       my_weaving_draft.getMultiplicity()-(y-10)/10); //Adjusted to fit new algorithm + display - David
               }
           //side
		   } else if(y >= 20 + (my_weaving_draft.getMultiplicity() * 10) &&  //in the vertical column
				   y < 20 + ((my_weaving_draft.getMultiplicity() + my_weaving_draft.getHeight()) * 10) ) {

               if(set)
			        my_weaving_draft.setVertical(
                       (y - 20 -(my_weaving_draft.getMultiplicity() * 10))/10,
					   (x-20-(my_weaving_draft.getWidth() * 10))/10+1);
		   }
	   }  	   
	   this.repaint();
   }
   public WeavingDraft getWeavingDraft(){
	   return my_weaving_draft;
   }
   public void setWeavingDraft(WeavingDraft the_draft)
   {
	   my_weaving_draft = the_draft;
   }

    private class WDMouseListener extends MouseAdapter {
        boolean mode_enable = false;
        boolean mode_disable = false;
        public void mouseDragged(MouseEvent e) {
            if(mode_enable || mode_disable) {
                if(mode_enable)
                    clickedSquare(e.getX(),e.getY(),true);
                else if(mode_disable)
                    clickedSquare(e.getX(),e.getY(),false);
            }
        }

        public void mouseMoved(MouseEvent e) {
            if(!(mode_enable || mode_disable)) {
                //TODO: hover effect
            }
        }

        public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1) {
                mode_enable = true;
                clickedSquare(e.getX(),e.getY(),true);
            }
            if(e.getButton() == MouseEvent.BUTTON3) {
                mode_disable = true;
                clickedSquare(e.getX(),e.getY(),false);
            }
        }

        public void mouseReleased(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1)
                mode_enable = false;
            if(e.getButton() == MouseEvent.BUTTON2)
                mode_disable = false;
        }
    }
}
